#!/anaconda3/envs/Script/bin python3.8
# -*- coding: utf-8 -*-
# ---
# @Software: PyCharm
# @File: log.py
# @Author: AllenFu
# @Institution: NCHU, China
# @E-mail: nchufujianjian@126.com
# @Site: 
# @Time: 11月 27, 2020
# ---
from sqlalchemy import desc, asc
from sqlalchemy_serializer import SerializerMixin

from app import dBSession
from app.models.base_model import BaseModel
from app.models.model import HtLog
from app.vendor.decorator import classTransaction


class Log(HtLog, BaseModel, SerializerMixin):
    def getOne(self, filters, order='id desc', field=()):
        res = dBSession.query(Log).filter(*filters)
        order = order.split(' ')
        if order[1] == 'desc':
            res = res.order_by(desc(order[0])).first()
        else:
            res = res.order_by(asc(order[0])).first()
        if res == None:
            return None
        if not field:
            res = res.to_dict()
        else:
            res = res.to_dict(only=field)
        return res

    """
        添加
        @param obj data 数据
        @return bool
    """

    @classTransaction
    def add(self, data):
        log = Log(**data)
        dBSession.add(log)
        dBSession.flush()
        return log.id